import { addHistory, getGoodsDetail_l , collectGoods } from '../../service/public.js'
import { showToast } from '../../utils/asyncWX'
import { runtime } from '../../utils/runtime'
Page({
	data: {
		info: {},
    isCollect: false,
    gid:5
	},
	goodsId: '',
	swiper_img: [],
	goods_info: {},
	onShow() {
		let Pages = getCurrentPages()
		let curPage = Pages[Pages.length - 1]
		let { options } = curPage

		const goods_id = options.gid
    this.setData({
      gid:options.gid
    })
    this._getGoodsDetail(goods_id)
    
    // 当进入页面时增加浏览历史记录

    this._addHistory(goods_id)


	},
	_getGoodsDetail(id) {
	
		getGoodsDetail_l({ gid: id }).then((res) => {

      const info = res.data.data


			this.setData({
				info,
				// isCollect,
			})
		})
	},
	// 轮播图店家
	swiperClick(e) {
		const index = e.detail.index
		wx.previewImage({
			current: this.swiper_img[index],
			urls: this.swiper_img,
		})
	},
	// 加入购物车
	addCart() {
		let cart = wx.getStorageSync('cart') || []
		// console.log('object :>> ', cart);

		const index = cart.findIndex((item) => {
			return item.goods_id == this.goods_info.goods_id
		})
		let text = ''
		if (index === -1) {
			// 没有商品数据
			this.goods_info.count = 1
			this.goods_info.checked = true
			cart.push(this.goods_info)
			text = '加入购物车成功'
		} else {
			cart[index].count++
			text = '该商品数量+1'
		}
		wx.setStorageSync('cart', cart)

		wx.showToast({
			title: text,
			mask: true,
		})
	},
	// 收藏商品

	async collect() {
		// 1.获得本地中存储的商品列表
		let collects = wx.getStorageSync('collects') || []
		let index = collects.findIndex(
			(v) => v.goods_id === this.goods_info.goods_id
		)

		if (index !== -1) {
      collects.splice(index, 1)
      
      this._collectGoods(false)
			await showToast({ title: '取消收藏成功', icon: 'success' })
		} else {

      this._collectGoods(true) 

      collects.push(this.goods_info)

			await showToast({ title: '收藏成功', icon: 'success' })
		}

		this.setData({
			isCollect: index === -1 ? true : false,
		})

		wx.setStorageSync('collects', collects)
  },
  _addHistory(gid){

    addHistory({
      openid:wx.getStorageSync('openid') || '',
      gid,
    })
  },



})
